from django.db import models
from utils.base_model import BaseMixin


# Create your models here.
# 菜单
class Menu(BaseMixin):
    name = models.CharField(max_length=40)
    superior = models.IntegerField(verbose_name='上级菜单', null=True)  # 上级类目，如果为空，说明当前没有上级菜单，当前即为最顶级

    def __str__(self):
        return f'{self.superior}-{self.name}'

    class Meta:
        db_table = 'menu'


# 商品
class Commodity(BaseMixin):
    title = models.TextField('标题')
    subtitle = models.TextField('副标题')
    cover_picture = models.TextField('封面图')
    priority = models.IntegerField(default=100)  # 优先级（花钱买排名）越小优先级越高
    menu = models.ForeignKey('Menu', related_name='commodity',
                             on_delete=models.SET_NULL, null=True)  # 推荐使用'Menu',可以防止当类定义在当前类下方时，导致类名无法使用的情况
    column = models.ForeignKey('Column', related_name='commodity', on_delete=models.SET_NULL, null=True)

    def __str__(self):
        return f'{self.title}'

    class Meta:
        db_table = 'commodity'


# 栏目
class Column(BaseMixin):
    name = models.CharField(max_length=40)  # 栏目名，比如：猜你喜欢、好物推荐、双十一特惠，方便随时增减
    subtitle = models.CharField(max_length=40)  # 栏目副标题，比如：全名口碑，个性推荐
    priority = models.IntegerField(default=100)  # 优先级，决定哪个栏目在前，越小优先级越高
    status = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return f'{self.name}'

    class Meta:
        db_table = 'column'


# 商品选项
class CommodityDetail(BaseMixin):
    option_name = models.CharField(max_length=40)  # 选项名，eg：颜色分类
    options = models.TextField()  # 选项，eg：绿色|红色|蓝色
    price = models.FloatField(null=True)  # 价格
    inventory = models.IntegerField(null=True)  # 库存
    img = models.TextField(null=True)  # 商品图片
    status = models.IntegerField(default=0)
    create_time = models.DateTimeField(auto_now_add=True)
    update_time = models.DateTimeField(auto_now=True)
    commodity = models.ForeignKey('Commodity', on_delete=models.CASCADE)

    class Meta:
        db_table = 'commodity_detail'


# 商品图片
class CommodityImg(models.Model):
    src = models.TextField()  # 图片地址
    type = models.IntegerField()  # 图片类型(比如：0为展示处用图，1为详情处用图等)
    status = models.IntegerField(default=0)  # 图片状态
    priority = models.IntegerField(default=100)  # 优先级
    commodity = models.ForeignKey('Commodity', on_delete=models.CASCADE)

    class Meta:
        db_table = 'commodity_img'
